using Catalog.Application.Brands.Commands;
using Catalog.Application.Brands.Queries;
using Catalog.Application.Contracts.DTOs;
using Catalog.HttpAPI.Permissions;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Catalog.HttpAPI.Host.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    public class BrandController : ControllerBase
    {
        private readonly IMediator _mediator;


        public BrandController(IMediator mediator)
        {
            _mediator = mediator;
        }

        [AllowAnonymous]
        [HttpGet("seletct/{id}")]
        public async Task<ActionResult<BrandProfileDto>> GetByIdAsync([FromRoute] GetBrandByIdQuery query)
        {
            var brand = await _mediator.Send(query);
            if (brand == null)
            {
                return NotFound();
            }
            return Ok(brand);
        }

        [AllowAnonymous]
        [HttpGet("search")]
        public async Task<ActionResult<List<BrandProfileDto>>> GetListAsync([FromQuery] SearchBrandQuery query)
        {
            var brands = await _mediator.Send(query);
            return Ok(brands);
        }

        [Permission("brand:item:create", "创建品牌")]
        [Authorize(Policy = "brand:item:create")]
        [HttpPost("create")]
        public async Task<IActionResult> CreateAsync([FromBody] CreateBrandCommand command)
        {

            var result = await _mediator.Send(command);

            return Ok(result);
        }

        [Permission("brand:item:update", "更新品牌")]
        [Authorize(Policy = "brand:item:update")]
        [HttpPut("update")]
        public async Task<ActionResult> UpdateAsync([FromBody] UpdateBrandCommand command)
        {
            var result = await _mediator.Send(command);

            return Ok(result);
        }

        [Permission("brand:item:delete", "删除品牌")]
        [Authorize(Policy = "brand:item:delete")]
        [HttpDelete("delete/{id}")]
        public async Task<IActionResult> DeleteAsync([FromRoute] Guid id)
        {
            var command = new DeleteBrandCommand(id);
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [Permission("brand:item:name:change", "修改品牌名称")]
        [Authorize(Policy = "brand:item:name:change")]
        [HttpPatch("change-name")]
        public async Task<IActionResult> ChangeNameAsync([FromBody] ChangeBrandNameCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [Permission("brand:item:description:change", "修改品牌描述")]
        [Authorize(Policy = "brand:item:description:change")]
        [HttpPatch("change-description")]
        public async Task<IActionResult> ChangeDescriptionAsync([FromBody] ChangeBrandDescriptionCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    }
}